package org.myapp.event;

import java.util.HashMap;
import java.util.Map;

import org.myapp.event.MyListener.EventType;
import org.slf4j.LoggerFactory;

import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;

public final class EsperDemo {


	private Map<String, Integer> product = new HashMap<>();
	private static final org.slf4j.Logger LOGGER =
			LoggerFactory.getLogger(EsperDemo.class);
	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		
		EsperDemo esper = new EsperDemo();
		esper.productCatalog();
		
		LOGGER.info("Lancement de l'application");
		
		LOGGER.info("Fin de l'application");
		
		// lancement de exper
		Configuration config = new Configuration();
		config.addEventTypeAutoName("org.myapp.event"); // Raccourci pour les requêtes SQL
		EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(config);
		
		// lance une alerte
		//String avg_prive = "select avg(price) from OrderEvent.win:time(30 sec)"; // Trigger
		//EPStatement statement = epService.getEPAdministrator().createEPL(avg_price); // ajout du declencheur
		//statement.addListener(new MyListener(EventType.AVG_PRICE));
			
		// By attaching the listener to the statement the engine 
		//   provides the statement's results to the listener.
		LOGGER.info("Ajout du listener / déclencheur");
		
		String all_products = "select itemName, price from OrderEvent";
		EPStatement statement_0 = epService.getEPAdministrator().createEPL(all_products); 
		MyListener ecoute_commande = new MyListener(EventType.CMD_INVALID, esper.product);
		statement_0.addListener(ecoute_commande);
		statement_0.addListener(new MyListener(EventType.SAME_ITEM, esper.product));
		
		System.out.println(ecoute_commande.isValid());
		
		String price_more_1000 = "select itemName, price from OrderEvent where price>1000";
		EPStatement statement_1 = epService.getEPAdministrator().createEPL(price_more_1000); 
		statement_1.addListener(new MyListener(EventType.MORE_1000, esper.product));
		

		System.out.println(ecoute_commande.isValid());
		
		String calcul_rate = "select rate(5) as num_commande from OrderEvent.win:time(5 sec) output snapshot every 5 sec";
		EPStatement statement_2 = epService.getEPAdministrator().createEPL(calcul_rate); 
		statement_2.addListener(new MyListener(EventType.RATE_CMD, esper.product));
		
		LOGGER.info("Simule des évenements");
		
		Thread.sleep(5000);
		
		OrderEvent event = new OrderEvent("IPad", 657.50);
		epService.getEPRuntime().sendEvent(event);
		OrderEvent event1 = new OrderEvent("IPhone", 200);
		epService.getEPRuntime().sendEvent(event1);
		
		Thread.sleep(5000);
		
		OrderEvent event2 = new OrderEvent("IPhone", 640);
		epService.getEPRuntime().sendEvent(event2);
		OrderEvent event3 = new OrderEvent("IPod", -200);
		epService.getEPRuntime().sendEvent(event3);
		OrderEvent event4 = new OrderEvent("IMac", 1755);
		epService.getEPRuntime().sendEvent(event4);
		OrderEvent event5 = new OrderEvent("IPhone", 647);
		epService.getEPRuntime().sendEvent(event5);
		
		Thread.sleep(5000);
		
		OrderEvent event6 = new OrderEvent("AppleTv", 1500);
		epService.getEPRuntime().sendEvent(event6);
	}
	
	public void productCatalog(){
		product.put("IPod", 3);
		product.put("IPhone", 2);
		product.put("IPad", 1);
		product.put("IMac", 1);
	}
}
